package com.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pojo.Archive;
import com.pojo.Users;
import com.service.ArchiveService;
import com.service.UlogService;
import com.service.UsersService;

@Controller
public class UsersController {
	UsersService service=new UsersService();
	UlogService service2=new UlogService();
	ArchiveService service1=new ArchiveService();
	@RequestMapping(value="/login.action",method=RequestMethod.POST)
	@ResponseBody
	public String login(
			@RequestParam(value="account",defaultValue="") String account,
			@RequestParam(value="password",defaultValue="") String password,
			HttpServletResponse response,
			@CookieValue(value="us") String usId
			){
		System.out.println(account);
		System.out.println(password);
		Users up=service.getUser(account, password);
		//response.addCookie(new Cookie("us","100"));
		service2.ulogadd(usId, null, "登陆");
		//System.out.println(up);
		System.out.println(password);
		if("0".equals(up.getCounter())){
			return "nologin";
		}
		return "islogin";
	}
	@RequestMapping(value="/querynn.action",method=RequestMethod.GET)
	@ResponseBody
	public Users nickname(
			@CookieValue(value="us") String usId,
			HttpServletResponse response
			){
		response.addCookie(new Cookie("us","100"));
		Users user=service.querynn("100");
		return user;
	}
	@RequestMapping(value="/updatenn.action",method=RequestMethod.POST)
	@ResponseBody
	public String update(
			@CookieValue(value="us") String usId,
			@RequestParam(value="nickname") String nn
			){
		service.updatenn(nn,usId);
		return "OK";
	}
	@RequestMapping(value="/quit.action",method=RequestMethod.POST)
	@ResponseBody
	public String quit(
			@CookieValue(value="us",defaultValue="") String usId,
			HttpServletResponse response
			){
		Archive arc=service1.queryid();
		service2.ulogadd(usId, arc.getId(), "退出");
		response.addCookie(new Cookie("us", null));
		return "quit";}
}
